//
//  RankListVC.m
//  jingdongfang
//
//  Created by MTiMac01 on 2017/8/23.
//  Copyright © 2017年 LJL. All rights reserved.
//

#import "RankListVC.h"
#import "IGRankListApi.h"
#import "XiangQingViewController.h"
@interface RankListVC ()
{
    UIButton *titleBtn;
    UIView *topView;
    UIButton *monthBtn;
    UIButton *yearBtn;
    BOOL topViewIsShow;
}
@property (weak, nonatomic) IBOutlet UIView *backView;
@property (weak, nonatomic) IBOutlet UIButton *pushBtn;//推送榜
@property (weak, nonatomic) IBOutlet UIButton *collectBtn;//收藏榜
@property (weak, nonatomic) IBOutlet UIButton *hotBtn;//人气榜
@property (weak, nonatomic) IBOutlet UIView *grayView;//黑条
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *grayViewCenterY;
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@property (nonatomic, strong)IGRankListApi *api;
@property (nonatomic, strong)NSMutableArray *dataSource;

@end

@implementation RankListVC

- (void)viewDidLoad
{
    [super viewDidLoad];
    topViewIsShow = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self creatNav];
    [self creatTableView];
    self.api = [IGRankListApi new];
    self.dataSource = [@[] mutableCopy];
    self.type = 0;
    [self dataSource];
    
}

- (void)creatNav
{
    UIView *gray = [[UIView alloc] initWithFrame:CGRectMake(0, _grayView.bottom, KSCREENWIDTH, 0.5)];
    gray.backgroundColor = [UIColor c6Color];
    [_backView addSubview:gray];
//    self.title = @"月度排行榜";
    self.title = NSLocalizedString(@"Monthly Hits", nil);
}

- (void)creatTableView
{
    @KWeakObj(self)
    if ([[[UIDevice currentDevice] systemVersion] floatValue]>= 11.0)
    {
        self.myTableView.estimatedRowHeight = 0;
        self.myTableView.estimatedSectionHeaderHeight = 0;
        self.myTableView.estimatedSectionFooterHeight = 0;
    }
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.myTableView.backgroundColor = [UIColor whiteColor];
    [self.myTableView registerNib:[UINib nibWithNibName:@"RankListCell" bundle:nil]forCellReuseIdentifier:@"RankListCell"];
    [self.myTableView ig_setHeaderWithRefreshingBlock:^{
        [selfWeak dataRequest];
    }];
}

- (IBAction)pushBtnClick:(id)sender
{
    if (!self.pushBtn.selected)
    {
        self.type = pushType;
    }
}
- (IBAction)collectBtnClick:(id)sender
{
    if (!self.collectBtn.selected)
    {
        self.type = collectType;
    }
}
- (IBAction)hotBtnClick:(id)sender
{
    if (!self.hotBtn.selected)
    {
        self.type = hotType;
    }
}
- (void)setType:(RankListType)type
{
    if (_type != type)
    {
        _type = type;
    }

    if (self.type == 0)
    {
        [UIView animateWithDuration:0.2 animations:^{
            self.grayView.centerX = self.pushBtn.centerX;
        }];
        self.pushBtn.selected = YES;
        self.collectBtn.selected = NO;
        self.hotBtn.selected = NO;
    }
    else if (self.type == 1)
    {
        [UIView animateWithDuration:0.2 animations:^{
            self.grayView.centerX = self.collectBtn.centerX;
        }];
        self.pushBtn.selected = NO;
        self.collectBtn.selected = YES;
        self.hotBtn.selected = NO;
    }
    else if (self.type == 2)
    {
        [UIView animateWithDuration:0.2 animations:^{
            self.grayView.centerX = self.hotBtn.centerX;
        }];
        self.pushBtn.selected = NO;
        self.collectBtn.selected = NO;
        self.hotBtn.selected = YES;
    }
    [self dataRequest];
}
#pragma mark ==== 点击按钮请求数据
- (void)dataRequest
{
    NSInteger i = self.type;
    self.api.type = [NSString stringWithFormat:@"%ld", i + 1];
    self.api.dimension = @"m";
    [self.api startWithDecodeClass:[IGRankListResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        [_myTableView ig_endHeaderRefreshing];
        if (responseObject)
        {
            IGRankListResp *resp = responseObject;
            IGRankListModel *model = resp.data;
            [_dataSource removeAllObjects];
            if (model.rankingMonthList && model.rankingMonthList.count > 0)
            {
                _dataSource = [NSMutableArray arrayWithArray:model.rankingMonthList];
            }
            [self.myTableView reloadData];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.myTableView setContentOffset:CGPointMake(0, 0) animated:YES];
            });
        }
        else
        {
//            [self presentMessageTips:@"无内容"];
            [self presentMessageTips:NSLocalizedString(@"Null", nil)];
        }
    }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataSource.count ? self.dataSource.count : 0;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_dataSource.count)
    {
        RankListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RankListCell" forIndexPath:indexPath];
        cell.selectionStyle= UITableViewCellSelectionStyleNone;
        cell.rowNum = indexPath.section + 1;
        cell.list = _dataSource[indexPath.section];
        cell.type = self.type;
        return cell;
    }
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    IGRankList *list = _dataSource[indexPath.section];
    XiangQingViewController *vc = [[XiangQingViewController alloc] init];
    vc.p_id = list.p_id;
    vc.isRoot = YES;
    [UIViewController ig_pushViewController:vc animated:YES];
}


@end
